//
//  BaseViewController.swift
//  SwiftCode
//
//  Created by yuhus on 2024/1/24.
//

import Foundation
import RxSwift

open class BaseViewController: UIViewController {
  let bag = DisposeBag()
  
  private let isDebugPrint = false
  public let viewDidLoadSubject = ReplaySubject<Void>.create(bufferSize: 1)
  public let viewWillAppear = PublishSubject<Void>()
  public let viewDidAppear = ReplaySubject<Void>.create(bufferSize: 1)
  public let viewWillDisappear = PublishSubject<Void>()
  public let viewDidDisappear = PublishSubject<Void>()
  public let willMoveToParentSubject = PublishSubject<UIViewController?>()
    
  public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  }
  
  required public init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
  deinit {
    viewDidLoadSubject.onCompleted()
    viewWillAppear.onCompleted()
    viewDidAppear.onCompleted()
    viewWillDisappear.onCompleted()
    viewDidDisappear.onCompleted()
    willMoveToParentSubject.onCompleted()
  }
  
  var pageName: String {
    String(describing: self)
  }
  
  open override var modalPresentationStyle: UIModalPresentationStyle {
    didSet {
      if modalPresentationStyle == .overFullScreen {
        modalPresentationCapturesStatusBarAppearance = true
      }
    }
  }
  
  open override func viewDidLoad() {
    super.viewDidLoad()
    viewDidLoadSubject.onNext(())
    
    navigationController?.navigationBar.topItem?.title = ""
    navigationController?.navigationBar.tintColor = UIColor.neutral1
    navigationController?.setNavigationBarTranslucent(true)
    navigationController?.navigationBar.titleTextAttributes =
    [NSAttributedString.Key.foregroundColor: UIColor.neutral1, NSAttributedString.Key.font: UIFont.poppinsFont(ofSize: 18, weight: .bold)]
  }
  
  open override func willMove(toParent parent: UIViewController?) {
    super.willMove(toParent: parent)
    willMoveToParentSubject.onNext(parent)
  }
  
  open override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if isDebugPrint { Timber.i("viewWillAppear:\(pageName)") }
    viewWillAppear.onNext(())
  }
  
  open override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    if isDebugPrint { Timber.i("viewDidAppear:\(pageName)") }
    viewDidAppear.onNext(())
  }
  
  open override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    if isDebugPrint { Timber.i("viewWillDisappear:\(pageName)") }
    viewWillDisappear.onNext(())
  }
  
  open override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    viewDidDisappear.onNext(())
    if isDebugPrint { Timber.i("viewDidDisappear:\(pageName)") }
  }
  
  // 默认不显示状态栏
  open override var prefersStatusBarHidden: Bool { false }
  
  open override var preferredStatusBarStyle: UIStatusBarStyle { .darkContent }
  
  public func createTitleView(_ text: String?) -> UILabel {
    let label = UILabel()
    label.text = text
    label.textColor = .neutral1
    label.font = .poppinsFont(ofSize: 18.0, weight: .semiBold)
    return label
  }
  
  func dismissKeyboardWhenViewTapped() {
    view.rx.tapGesture()
      .when(.recognized)
      .subscribe(onNext: { [weak self] _ in
        self?.view.endEditing(true)
      })
      .disposed(by: bag)
  }
  
  open override var shouldAutorotate: Bool {
    return false
  }
}

extension UINavigationController {
  // 注意调用这个函数会移除原有NavigationBar的backgroundImage和shadowImage
  public func setNavigationBarTranslucent(_ isTranslucent: Bool) {
    navigationBar.setBackgroundImage(isTranslucent ? UIImage() : nil, for: .default)
    navigationBar.shadowImage = isTranslucent ? UIImage() : nil
    navigationBar.isTranslucent = isTranslucent
    view.backgroundColor = isTranslucent ? .clear : UINavigationBar.appearance().barTintColor
  }
}
